<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="活动状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动ID" align="center" prop="activityId" />
      <el-table-column label="活动名称" align="center" prop="title" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="location" />
      <el-table-column label="报名人数" align="center">
        <template slot-scope="scope">
          <el-progress :percentage="getEnrollmentPercentage(scope.row)" :format="format">
            <span>{{ scope.row.currentParticipants || 0 }}/{{ scope.row.maxParticipants || '不限' }}</span>
          </el-progress>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activity_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >查看详情</el-button>
          <el-button
            v-if="canEnroll(scope.row)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEnroll(scope.row)"
          >立即报名</el-button>
          <el-tag v-else-if="scope.row.status === '3'" type="info">已结束</el-tag>
          <el-tag v-else-if="isEnrollmentFull(scope.row)" type="warning">已满</el-tag>
          <el-tag v-else type="info">未开始</el-tag>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 活动详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-descriptions class="margin-top" :column="2" border>
        <el-descriptions-item label="活动名称">{{ form.title }}</el-descriptions-item>
        <el-descriptions-item label="活动地点">{{ form.location }}</el-descriptions-item>
        <el-descriptions-item label="开始时间">{{ parseTime(form.startTime) }}</el-descriptions-item>
        <el-descriptions-item label="结束时间">{{ parseTime(form.endTime) }}</el-descriptions-item>
        <el-descriptions-item label="报名人数">
          {{ form.currentParticipants || 0 }}/{{ form.maxParticipants || '不限' }}
        </el-descriptions-item>
        <el-descriptions-item label="活动状态">
          <dict-tag :options="dict.type.activity_status" :value="form.status"/>
        </el-descriptions-item>
        <el-descriptions-item label="活动描述" :span="2">
          {{ form.description }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
        <el-button v-if="canEnroll(form)" type="primary" @click="handleEnroll(form)">立即报名</el-button>
      </div>
    </el-dialog>

    <!-- 报名对话框 -->
    <el-dialog :title="'报名活动 - ' + enrollForm.title" :visible.sync="enrollOpen" width="500px" append-to-body>
      <el-form ref="enrollForm" :model="enrollForm" :rules="enrollRules" label-width="80px">
        <el-form-item label="活动名称">
          <span>{{ enrollForm.title }}</span>
        </el-form-item>
        <el-form-item label="活动时间">
          <span>{{ parseTime(enrollForm.startTime) }} 至 {{ parseTime(enrollForm.endTime) }}</span>
        </el-form-item>
        <el-form-item label="活动地点">
          <span>{{ enrollForm.location }}</span>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="enrollForm.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="enrollForm.remark" type="textarea" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEnroll">确 定</el-button>
        <el-button @click="enrollOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivityList, getActivityDetail, enrollActivity } from "@/api/education/activityList";

export default {
  name: "ActivityList",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      activityList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        status: null
      },
      // 详情对话框
      detailOpen: false,
      // 详情表单
      detailForm: {},
      // 报名对话框
      enrollOpen: false,
      // 报名表单
      enrollForm: {},
      // 报名表单校验
      enrollRules: {
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ]
      },
      // 活动状态字典
      dict: {
        type: {
          activity_status: [
            { label: '待审核', value: '0' },
            { label: '已通过', value: '1' },
            { label: '已拒绝', value: '2' },
            { label: '已结束', value: '3' }
          ]
        }
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listActivityList(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      const activityId = row.activityId;
      getActivityDetail(activityId).then(response => {
        this.detailForm = response.data;
        this.detailOpen = true;
      });
    },
    /** 报名按钮操作 */
    handleEnroll(row) {
      this.enrollForm = {
        activityId: row.activityId,
        title: row.title,
        startTime: row.startTime,
        endTime: row.endTime,
        location: row.location,
        phone: '',
        remark: ''
      };
      this.enrollOpen = true;
    },
    /** 提交报名 */
    submitEnroll() {
      this.$refs["enrollForm"].validate(valid => {
        if (valid) {
          const data = {
            activityId: this.enrollForm.activityId,
            phone: this.enrollForm.phone,
            remark: this.enrollForm.remark
          };
          enrollActivity(data).then(response => {
            this.$modal.msgSuccess("报名成功");
            this.enrollOpen = false;
          });
        }
      });
    },
    /** 计算报名百分比 */
    getEnrollmentPercentage(row) {
      if (!row.maxParticipants) return 0;
      return Math.round((row.currentParticipants || 0) / row.maxParticipants * 100);
    },
    /** 格式化进度条显示 */
    format(percentage) {
      return '';
    },
    /** 判断是否可以报名 */
    canEnroll(row) {
      if (row.status !== '1') return false;
      if (this.isEnrollmentFull(row)) return false;
      const now = new Date().getTime();
      const start = new Date(row.startTime).getTime();
      const end = new Date(row.endTime).getTime();
      return now >= start && now <= end;
    },
    /** 判断是否报名已满 */
    isEnrollmentFull(row) {
      return row.maxParticipants && row.currentParticipants >= row.maxParticipants;
    },
    // ... existing methods ...
  }
};
</script>

<style scoped>
.el-progress {
  margin-right: 5px;
}
</style> 